//index.js
//获取应用实例
const app = getApp()
var page = 1;
var post_list = [];
let loadingMore = false;
let loadedEnd = false;
Page({
  data: {
    userInfo: {},
    user_id:'',
    post:[],
    plate_id:'',
    plate:{},
    adv:null
  },
  onReady: function () {
    this.post = this.selectComponent("#post");
  },
  onLoad:function(option){
    this.setData({
      plate_id:option.plate_id
    })
  },
  //事件处理函数
  onShow: function () {
    var value = wx.getStorageSync('user_id');
    page = 1;
    post_list = [];
    if(value&&this.data.plate_id){
      this.getPostList(page);
    }else{
      app.login()
      .then(app.doLogin)
      .then(app.checkAuth('scope.userInfo'))
      .then(app.getUserInfo)
      .then(app.setUserData)
      .then(this.getPostList(page))
      .catch(app.toLoad)
    }
  },
  getPostList:function(page){
    let that = this;
    console.log(that.data.plate_id)
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'https://rtutrip.aihuodong.top/index/index/get_post_list',
        data: {
          plate_id:that.data.plate_id,
          page:page
        },
        success (res) {
          console.log(res.data.data)
          if (res.data.data.post.length == 0&&page!=1) {
            loadedEnd = true;
            wx.showToast({
              title: "已经到底啦",
              icon: "success"
            });
          }
          for (var i = 0; i < res.data.data.post.length; i++) {
            post_list.push(res.data.data.post[i]);
          }
          if(res.data.data.ad){
            that.setData({
              adv:res.data.data.ad
            })
          }
          that.setData({
            post:post_list,
            plate:res.data.data.plate,
          })
          resolve(res);
        }
      })
    })
  },
  onReachBottom() {
    if (loadingMore || loadedEnd) return false;
    loadingMore = true;
    wx.showToast({
      title: "数据加载中",
      icon: "loading"
    });
    page += 1;
    this.getPostList(page);
    loadingMore = false;
  },
  onShareAppMessage: function (res) {
    const _this = this
    return {
      title: _this.data.plate.plate_name,
      path: '/pages/post/post?plate_id=' + _this.data.plate_id
    }
  }
})
